<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\CompanyModel;
use Illuminate\Http\Request;

class CompanyController extends Controller
{
    public function page(Request $request) {
        $companies = CompanyModel::page($request->input('keywords', ''));
        return self::sendOk($companies, '获取成功', 200);
    }

    public function add(Request $request) {
        // 实现添加逻辑
        $data = [
            'name' => $request->input('name'),
            'parent_id' => $request->input('parent_id', 0),
            'type' => $request->input('type', 0),
            'end_date' => $request->input('end_date', null),
            'address' => $request->input('address'),
            'contact_user' => $request->input('contact_user'),
            'contact_phone' => $request->input('contact_phone'),
            'status' => $request->input('status', 0)
        ];

        // 检查公司名称是否已存在
        $existingCompany = CompanyModel::where('name', $data['name'])->first();
        if ($existingCompany) {
            return self::sendError('公司名称已存在', 400);
        }

        // 处理logo文件上传
        $logoFile = $request->file('logo');
        if ($logoFile) {
            $logoPath = $logoFile->store('logos', 'public');
            $data['logo'] = $logoPath;
        }
        $company = CompanyModel::add($data);

        if ($company) {
            return self::sendOk($company, '添加成功', 200);
        }
    }

    public function edit(Request $request) {
        
    }

    public function delete(Request $request) {
        
    }

    public function detail(Request $request) {
        
    }
}
